﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Text.RegularExpressions
Imports FL_8x0.Common
Imports FL_8x0.Model
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.Viewmodel

    Public Class LogViewModel : Inherits ViewModelBase

        Private _logList As DataView             '日志列表视图
        Private _logModel As LogModel            '日志查询参数
        Private _loading As Visibility
        Private _searchCommand As RelayCommand   '点击“查询”触发命令
        Private _resetCommand As RelayCommand    '点击“取消”触发命令


        Public Sub New()
            LogModel = New LogModel()
            FlashShowTable()
            SearchCommand = New RelayCommand(Sub() ExecQuery())
            ResetCommand = New RelayCommand(Sub() ResetQuery())
        End Sub


        '=====================================================================
        '函 数 名: ExecQuery
        '参    数: 
        '返 回 值:
        '功能描述: 判断参数有效性 执行查询
        '=====================================================================
        Private Sub ExecQuery()

            If LogModel.Id <> "" Then
                If Not IsNumeric(LogModel.Id) Then
                    log.WriteLogError("输入参数检查", "日志号[ID]只能含[数字]！")
                    Return
                End If
            End If

            Dim rg1 As Regex = New Regex("[=]") '典型的SQL注入攻击 [' or '1' = '1]

            If LogModel.Level <> "" Then
                If rg1.IsMatch(LogModel.Level) Then
                    log.WriteLogError("输入参数检查", "等级[Level]非法输入格式！")
                    Return
                End If
            End If

            If LogModel.Source <> "" Then
                If rg1.IsMatch(LogModel.Source) Then
                    log.WriteLogError("输入参数检查", "来源[Source]非法输入格式！")
                    Return
                End If
            End If

            If LogModel.UserName <> "" Then
                If Not LogModel.UserName = SUPER_USER_NAME And Not CheckUserExist(LogModel.UserName) Then
                    log.WriteLogError("输入参数检查", "[用户名]不存在！")
                    Return
                End If
            End If

            If LogModel.StartDate <> "" And LogModel.EndDate <> "" Then
                If CDate(LogModel.StartDate) > CDate(LogModel.EndDate) Then
                    log.WriteLogError("输入参数检查", "[日志时间]起始日期不能大于结束日期！")
                    Return
                End If
            End If

            FlashShowTable()
        End Sub


        '=====================================================================
        '函 数 名: ResetQuery
        '参    数: 
        '返 回 值:
        '功能描述: 清空查询产数 执行查询
        '=====================================================================
        Private Sub ResetQuery()
            LogModel = New LogModel()
            Call FlashShowTable()
        End Sub

        '=====================================================================
        '函 数 名: FlashShowTable
        '参    数: 
        '返 回 值:
        '功能描述: 查询显示
        '=====================================================================
        Public Async Function QueryTable() As Task(Of DataView)
            Return Await Task.Run(Function()
                                      Dim strConn As String = "Provider=" & SystemData.g_strDatabaseProvider & ";Data Source=" &
                                        log.LogPath & "\日志记录.mdb" &
                                        ";Persist Security Info=False;Jet OLEDB:Database Password=" & SUPER_USER_NAME &
                                        SUPER_PASSWORD
                                      Dim strWhere As String = ""

                                      If LogModel.Id <> "" Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "ID=" & LogModel.Id & ""
                                      End If

                                      If LogModel.UserName <> "" Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "UserName='" & LogModel.UserName & "'"
                                      End If

                                      If LogModel.Level <> "" Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "Level='" & LogModel.Level & "'"
                                      End If

                                      If LogModel.Source <> "" Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "Source='" & LogModel.Source & "'"
                                      End If

                                      If Not String.IsNullOrEmpty(LogModel.StartDate) Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "LogDate > #" & CDate(LogModel.StartDate) & "#"
                                      End If

                                      If Not String.IsNullOrEmpty(LogModel.EndDate) Then
                                          strWhere &= IIf(strWhere <> "", " and ", "") & "LogDate < #" & CDate(LogModel.EndDate).AddDays(1) & "#"
                                      End If


                                      Dim strCmd As String = "SELECT * FROM 日志记录"

                                      If strWhere <> "" Then
                                          strCmd &= " WHERE " & strWhere & " ORDER BY ID ASC"
                                      Else
                                          strCmd &= " ORDER BY ID ASC"
                                      End If

                                      Dim dbAdapter As New OleDbDataAdapter
                                      Dim dbTable As New DataTable

                                      Try
                                          dbAdapter = New OleDbDataAdapter(strCmd, strConn)
                                          dbAdapter.Fill(dbTable)

                                          Return dbTable.DefaultView
                                      Catch ex As Exception
                                          log.WriteLogError("", ex.Message)
                                          Return New DataView()
                                      Finally
                                          dbAdapter.Dispose()
                                      End Try
                                  End Function)
        End Function


        Public Async Sub FlashShowTable()
            Loading = Visibility.Visible
            LogList = Await QueryTable()
            Loading = Visibility.Collapsed
        End Sub


        Public Property LogList As DataView
            Get
                Return _logList
            End Get
            Set
                _logList = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property LogModel As LogModel
            Get
                Return _logModel
            End Get
            Set
                _logModel = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property SearchCommand As RelayCommand
            Get
                Return _searchCommand
            End Get
            Set
                _searchCommand = Value
            End Set
        End Property

        Public Property ResetCommand As RelayCommand
            Get
                Return _resetCommand
            End Get
            Set
                _resetCommand = Value
            End Set
        End Property

        Public Property Loading As Visibility
            Get
                Return _loading
            End Get
            Set(value As Visibility)
                _loading = value
                RaisePropertyChanged()
            End Set
        End Property
    End Class
End Namespace